package com.sinodata.bsm.center.service.config;

import java.util.List;

import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.common.vo.PageConf;
import com.sinodata.bsm.common.vo.PageModuleDef;
import com.sinodata.bsm.common.vo.PagePortal;

@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class PortalService {
    @Autowired
    private BaseDao baseDao;

    /**
     * 获取所有页面组件
     * 
     * @return
     */
    public List<PageModuleDef> findAll() {

        return (List<PageModuleDef>) baseDao.findListByHql("from PageModuleDef order by typename,title");
    }

    public PagePortal findUserPortalById(Long id) {
        return (PagePortal) baseDao.findById(PagePortal.class, id);
    }

    public PagePortal findPortalPageByUser(String userId, String pageId) {

        return (PagePortal) baseDao.findObjectByHql(PagePortal.class, " from PagePortal where userAccount='" + userId + "' and portalPage='" + pageId + "'");
    }

    @SuppressWarnings("unchecked")
    public List findListBySql(String sql) {

        return baseDao.getHibernateSession().createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
    }

    /**
     * 执行一个sql方法，不返回值
     * @param sql
     */
    public void executeSql(String sql) {

        baseDao.getHibernateSession().createSQLQuery(sql).executeUpdate();
    }

    public PageModuleDef findPageModuleDefId(String moduleId) {
        return (PageModuleDef) baseDao.findById(PageModuleDef.class, moduleId);
    }

    public PageConf findPageConfById(Long confId) {
        return (PageConf) baseDao.findById(PageConf.class, confId);
    }

    public List<PageConf> findPageConfByPagePortal(Long pagePortalId) {
        return (List<PageConf>) baseDao.findListByHql("from PageConf where pagePortalId=" + pagePortalId + " order by posx, posy ");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deletePageConf(PageConf conf) {
        baseDao.delete(conf);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void saveOrUpdatePageConf(PageConf pageConf) {
        baseDao.saveOrUpdate(pageConf);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void saveOrUpdatePagePortal(PagePortal portalPage) {
        baseDao.saveOrUpdate(portalPage);
    }

}
